/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import Facade.AgendaFacade;
import VO.AgendaVO;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import vista.AgendaBean;

/**
 *
 * @author Jorge
 */
public class AgendaAction extends DispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String BACK = "back";
    private final static String MENU = "menu";
    private final static String ADD = "add";
    private final static String EDIT = "edit";
    private final static String DEL = "del";
    private final static String ALL = "all";
    
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward volver(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return mapping.findForward(BACK);
    }
      public ActionForward Cancelar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(MENU);
    }

      public ActionForward ingresarC(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(ADD);
    }

      public ActionForward Agregar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

          AgendaBean frm = (AgendaBean)form;
          AgendaVO nuevo = new AgendaVO();
          AgendaFacade fcd = new AgendaFacade();

          nuevo.setNombre(frm.getNombre());
          nuevo.setFono(frm.getFono());

          if(nuevo.getFono().isEmpty()|| nuevo.getNombre().isEmpty())
          {
             String aviso = "<span style='color:red;font-weight:bold'>Debe ingresar todos los campos</span>";
            request.setAttribute("aviso", aviso);
          }else{
          if(fcd.addContacto(nuevo))
          {
             String aviso = "<span style='color:green;font-weight:bold'>Ingreso exitoso</span>";
            request.setAttribute("aviso", aviso);
          }
          }
         return mapping.findForward(ADD);
    }

       public ActionForward modificarC(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
           AgendaFacade fcd = new AgendaFacade();
           Vector contactos = fcd.cargarContactos();

           request.setAttribute("listaContactos", contactos);

        return mapping.findForward(EDIT);
       }
        public ActionForward Seleccionar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            AgendaFacade fcd = new AgendaFacade();
           AgendaVO seleccionado = new AgendaVO();
            Vector contactos = fcd.cargarContactos();
            seleccionado = fcd.cargarContacto(request.getParameter("cmbContactos"));
           


           request.setAttribute("listaContactos", contactos);
           request.setAttribute("datosContacto", seleccionado);

        return mapping.findForward(EDIT);
    }
        public ActionForward Editar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            AgendaBean frm = (AgendaBean)form;
            AgendaFacade fcd = new AgendaFacade();
            AgendaVO editado = new AgendaVO();
            editado.setNombre(frm.getNombre());
            editado.setFono(frm.getFono());
           Vector contactos = fcd.cargarContactos();

           request.setAttribute("listaContactos", contactos);
           request.setAttribute("datosContacto", editado);
           if(editado.getFono().isEmpty())
           {
                String aviso = "<span style='color:red;font-weight:bold'>Debe ingresar un telefono</span>";
                request.setAttribute("aviso", aviso);
           }else{
            if(fcd.editContacto(editado))
            {
                String aviso = "<span style='color:green;font-weight:bold'>Edición exitosa</span>";
                 request.setAttribute("aviso", aviso);
            }
           }
        return mapping.findForward(EDIT);
    }
         public ActionForward eliminarC(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
           AgendaFacade fcd = new AgendaFacade();
           Vector contactos = fcd.cargarContactos();

           request.setAttribute("listaContactos", contactos);

        return mapping.findForward(DEL);
       }
          public ActionForward Eliminar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
           AgendaFacade fcd = new AgendaFacade();
          

           if(fcd.borrarContacto(request.getParameter("cmbContactos")))
           {
            String aviso = "<span style='color:green;font-weight:bold'>Borrado exitoso</span>";
                request.setAttribute("aviso", aviso);
           }else{
           String aviso = "<span style='color:red;font-weight:bold'>No se podido eliminar el contacto</span>";
                request.setAttribute("aviso", aviso);
           }


           Vector contactos = fcd.cargarContactos();

           request.setAttribute("listaContactos", contactos);

        return mapping.findForward(DEL);
       }
          public ActionForward consultarC(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
           AgendaFacade fcd = new AgendaFacade();
           Vector contactos = fcd.cargarDatosContactos();

           request.setAttribute("listaContactos", contactos);

        return mapping.findForward(ALL);
       }
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward myAction2(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return mapping.findForward(SUCCESS);
    }
}
